<template>
  <div class="homePage">
    <NoticeBoard></NoticeBoard>
    <div class="main"
         v-loading="loading"
         element-loading-text="获取数据中……"
         element-loading-spinner="el-icon-loading">
      <div class="home">
        <div v-if="list.length <=0">
          <el-empty description="空空如也……"></el-empty>
        </div>
        <div class="items" v-else>
          <div class="item" v-if="list" v-for="item in list" :key="item.id">
            <SingleNotice :notice="item"></SingleNotice>
          </div>
        </div>
      </div>
      <div class="pages">
        <el-pagination
          background
          layout="prev, pager, next"
          :page-size="8"
          :total="total"
          :current-page="pageNum"
          @current-change="handleCurrentChange">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import SingleNotice from '../components/SingleNotice';
import NoticeBoard from '../components/NoticeBoard';
import qs from 'qs';

export default {
  name: 'Home',
  components: {
    SingleNotice,
    NoticeBoard
  },
  data () {
    return {
      list: [],
      total: 0,
      pageNum: 1,
      loading: false,//上传loading提示
    }
  },
  created () {
    this.getData(0,8);
  },
  methods:{
    getData(offset,limit){
      this.loading = true
      this.axios.post('/php/select.php', qs.stringify({
        offset: offset,
        limit: limit,
        type: '失物招领'
      }), { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).then((res) => {
        if(res.data === 0){
          this.total = 0;
          this.list = [];
          this.loading = false;
          return;
        }
        this.total = res.data.total
        this.list = res.data.data
        this.loading = false
      }).catch((err) => {
        this.loading = false
        this.$message.error(err)
      })
    },
    handleCurrentChange(val){
      this.list = [] //清空上一页数据
      this.getData((val-1)*8,8);
    }
  }
}
</script>

<style scoped>

</style>
